﻿using Back_end.Models;
using Microsoft.AspNet.Identity.Owin;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Back_end.Controllers
{
	[Authorize(Roles = "SuperUser, Admin")]
	public class ComplaintsController : Controller
	{
		private ApplicationDbContext db = new ApplicationDbContext();
		private ApplicationUserManager _userManager;

		public ApplicationUserManager UserManager
		{
			get
			{
				return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
			}
			private set
			{
				_userManager = value;
			}
		}



		// GET: Complaints
		//public ActionResult Index()
		//{
		//	return View(db.Complaints.ToList());
		//}

		public ActionResult Index(string search, string genre)
		{
			//var complaints = from c in db.Complaints
			//				 select c;
			//if (!string.IsNullOrWhiteSpace(search))
			//	complaints = complaints.Where(c => c.Text.Contains(search) || c.Tag.Contains(search));

			//return View(complaints);

			var users = from u in db.Complaints
						orderby u.Weight
						select u;

			if (!string.IsNullOrWhiteSpace(search))
			{
				ViewBag.Sub = genre;
				if (genre == "发布者")
					return View(users.Where(u => u.UserName.Contains(search)));
				else if (genre == "所在地")
					return View(users.Where(u => u.Location.Contains(search)));
				else if (genre == "公司")
					return View(users.Where(u => u.Company.Contains(search)));
				else
					return View(users.Where(u => u.Text.Contains(search)));
			}

			ViewBag.Title = search + " - 搜索结果";
			ViewBag.Sub = null;
			return View(users);
		}

		// GET: Complaints/Details/5
		public ActionResult Details(string id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			Complaint complaint = db.Complaints.Find(id);
			if (complaint == null)
			{
				return HttpNotFound();
			}

			ViewBag.Followed = db.FollowedList.Count(f => f.ComplaintId == id);
			ViewBag.Liked = db.Likes.Count(l => l.ComplaintId == id);
			return View(complaint);
		}

		// GET: Complaints/Create
		//public ActionResult Create()
		//{
		//	return View();
		//}

		// POST: Complaints/Create
		// 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
		// 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
		//[HttpPost]
		//[ValidateAntiForgeryToken]
		//public ActionResult Create([Bind(Include = "ComplaintId,UserName,PublishedDate,Text,PicturePath,Location,Tag,AllowedPublished,HadProcessed,Review,Weight")] Complaint complaint)
		//{
		//	if (ModelState.IsValid)
		//	{
		//		db.Complaints.Add(complaint);
		//		db.SaveChanges();
		//		return RedirectToAction("Index");
		//	}

		//	return View(complaint);
		//}

		// GET: Complaints/Edit/5
		public ActionResult Edit(string id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			Complaint complaint = db.Complaints.Find(id);
			if (complaint == null)
			{
				return HttpNotFound();
			}

			ViewBag.Followed = db.FollowedList.Count(f => f.ComplaintId == id);
			ViewBag.Liked = db.Likes.Count(l => l.ComplaintId == id);
			return View(complaint);
		}

		// POST: Complaints/Edit/5
		// 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
		// 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Edit([Bind(Include = "ComplaintId,UserName,PublishedDate,Text,PicturePath,Location,Coordinate,Tag,Company,AllowedPublished,HadProcessed,Review,Weight")] Complaint complaint)
		{
			if (ModelState.IsValid)
			{
				if (complaint.AllowedPublished == null ||
					complaint.AllowedPublished == false)
				{
					complaint.HadProcessed = null;
					complaint.Review = null;
					db.Entry(complaint).State = EntityState.Modified;
					db.SaveChanges();

					return RedirectToAction("Index");
				}
				if (complaint.HadProcessed == null ||
					complaint.HadProcessed == false)
				{
					complaint.Review = null;
					db.Entry(complaint).State = EntityState.Modified;
					db.SaveChanges();

					return RedirectToAction("Index");
				}

				var user = db.Users.Where(u => u.UserName == complaint.UserName).First();
				user.TotleCredit += 5;
				user.Credit += 5;
				db.Entry(complaint).State = EntityState.Modified;
				db.SaveChanges();
				return RedirectToAction("Index");
			}
			return View(complaint);
		}

		// GET: Complaints/Delete/5
		public async Task<ActionResult> Delete(string id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			Complaint complaint = db.Complaints.Find(id);
			var user = await UserManager.FindByNameAsync(complaint.UserName);
			user.Complaints.Remove(complaint);

			if (complaint == null)
			{
				return HttpNotFound();
			}
			return View(complaint);
		}

		// POST: Complaints/Delete/5
		[HttpPost, ActionName("Delete")]
		[ValidateAntiForgeryToken]
		public ActionResult DeleteConfirmed(string id)
		{
			Complaint complaint = db.Complaints.Find(id);
			db.Complaints.Remove(complaint);
			db.SaveChanges();
			return RedirectToAction("Index");
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				db.Dispose();
			}
			base.Dispose(disposing);
		}
	}
}
